ctrl + Fで検索fig.widthとfig.heightでhtml上での図の大きさ一括指定
# 全体
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE,
fig.width = 5, fig.height = 4)
displ: エンジンの排気量(リットル)hwy: 燃料効率(miles per gallon[mpg])library(tidyverse)
p <-
ggplot(mpg, aes(x = displ, y = hwy))
p + geom_point()
p +
geom_point(size = 1.5)
p +
geom_point(size = 3)
p +
geom_point(size = 5)
p +
geom_point(alpha = 0.1)
p +
geom_point(alpha = 0.5)
p +
geom_point(alpha = 0.7)
vignette("ggplot2-specs")のshapeを参考p +
geom_point(shape = "triangle")
library(ggimage)
p +
geom_image(aes(image = "image/kujira.PNG"))
p +
geom_point() +
labs(
title = "タイトル",
subtitle = "サブタイトル",
caption = "キャプション(短い説明文)"
)
p +
geom_point() +
labs(
title = "タイトル",
subtitle = "サブタイトル",
caption = "キャプション(短い説明文)"
) +
theme(
plot.title = element_text(size = 30),
plot.subtitle = element_text(size = 20),
plot.caption = element_text(size = 15)
)
p +
geom_point() +
labs(
title = "タイトル") +
theme(
plot.title = element_text(size = 30,
face = "bold", # 太字
color = "darkblue") # 色
)
p +
geom_point() +
labs(
title = "タイトル",
subtitle = "サブタイトル",
caption = "キャプション(短い説明文)"
) +
theme(
plot.title = element_text(hjust = 0.5), # 水平方向0-1の範囲で相対的な位置を指定
plot.subtitle = element_text(hjust = 0.5),
plot.caption = element_text(hjust = 0)
)
windowsFonts("mincho" = windowsFont("MS Mincho"))
p +
geom_point() +
labs(title = "Title タイトル たいとる 題名") +
theme(plot.title = element_text(family = "mincho",
size = 20))
windowsFonts("MEI" = windowsFont("Meiryo"))
p +
geom_point() +
labs(title = "Title タイトル たいとる 題名") +
theme(plot.title = element_text(family = "MEI",
size = 20))
p +
geom_point() +
labs(title = "Title タイトル たいとる 題名") +
theme(plot.title = element_text(family = "serif",
size = 20))
p +
geom_point() +
labs(title = "Title タイトル たいとる 題名") +
theme(plot.title = element_text(family = "sans",
size = 20))
p +
geom_point() +
labs(title = "Title タイトル たいとる 題名") +
theme(plot.title = element_text(family = "mono",
size = 20))
x = ""のようにしてもできるp +
geom_point() +
labs(
x = "x軸の名前",
y = "y軸の名前"
)
p +
geom_point() +
labs(
title = "Title タイトル たいとる 題名",
x = "hwy_x軸の名前",
y = "displ_y軸の名前"
) +
theme(axis.text = element_text(family = "serif",
size = 15))
p +
geom_point() +
labs(
title = "Title タイトル たいとる 題名",
x = "hwy_x軸の名前",
y = "displ_y軸の名前"
) +
theme(axis.title = element_text(family = "serif",
size = 15))
mpg %>%
select(displ, hwy) %>% # displとhwyを選んでsummary()に渡す
summary()
## displ hwy
## Min. :1.600 Min. :12.00
## 1st Qu.:2.400 1st Qu.:18.00
## Median :3.300 Median :24.00
## Mean :3.472 Mean :23.44
## 3rd Qu.:4.600 3rd Qu.:27.00
## Max. :7.000 Max. :44.00
scale_x_continuous()およびscale_y_continuous()のbreaksで指定limits =p +
geom_point() +
scale_x_continuous(
breaks = seq(from = 1, to = 8, by = 1), # 1から8まで1ずつ増える
limits = c(1, 8)) + # 1を表示したい場合
scale_y_continuous(
breaks = seq(from = 10, to = 45, by = 5), # 10から45まで5ずつ増える
limits = c(10, 45))
p +
geom_point() +
scale_y_continuous(
breaks = c(15, 30, 45))
p +
geom_point() +
theme(axis.text = element_text(size = 9)) # 単位はpt
p +
geom_point() +
theme(axis.text = element_text(size = 12)) # 単位はpt
p +
geom_point() +
theme(axis.text = element_text(size = 16)) # 単位はpt
p +
geom_point() +
labs(
title = "Title タイトル たいとる 題名",
x = "hwy_x軸の名前",
y = "displ_y軸の名前"
) +
theme(text = element_text(family = "serif",
size = 15))
p +
geom_point() +
geom_hline(yintercept = 30, # 座標の位置
linetype = "dotted", # 点線
color = "red", # 色
size = 1) # 大きさ
p +
geom_point() +
geom_vline(xintercept = 5,
linetype = "dotted",
color = "red",
size = 1)
p +
geom_point() +
annotate(geom = "text",
x = 5, y = 35, # テキストの中心座標位置
label = "ここにテキスト",
color = "blue",
size = 10)
p +
geom_point() +
annotate(geom = "text",
x = 5, y = 35, # テキストの中心座標位置
label = "ここにテキスト",
color = "blue",
size = 9,
angle = 90)
p +
geom_point() +
annotate(geom = "text",
x = 5, y = 35, # テキストの中心座標位置
label = "text テキスト",
size = 10,
family = "serif")
italic()で囲む=をテキスト内に表示する場合==と入れるparse = TRUEを入れる# 相関係数の計算
corc <-
cor(mpg$displ, mpg$hwy) %>%
round(2)
p +
geom_point() +
annotate("text",
label = str_c("italic(r) == ", corc), # 相関係数の代入
parse = TRUE,
x = 6, y = 40,
size = 6)
coord_cartesian(clip = "off")を使って、グラフの灰色背景部分をxlim =,ylim =の座標で指定した領域までに表示制限する必要があるp +
geom_point() +
annotate(geom = "text",
x = 0.5, y = 7, # テキストの中心座標位置
color = "blue",
sie = 10,
label = "テキスト") +
coord_cartesian(xlim = c(1, 7.2),
ylim = c(11, 44),
clip = "off")
cowplotを使用library(cowplot)
ppc <-
p + geom_point()
ggdraw(ppc) +
draw_label("text",
colour = "blue",
size = 20,
x = 0.1,
y = 0.2
)
magickパッケージのインストールが必要ggdraw(ppc) +
draw_image("image/kujira.png",
x = 0.5, y = 0.6,
width = 0.3, height = 0.3)
p +
geom_point() +
geom_smooth()
p +
geom_point() +
geom_smooth(method = "lm")
p +
geom_point() +
geom_smooth(method = "lm", se = FALSE)
p +
geom_point() +
theme_bw()
p +
geom_point() +
theme_classic()
library(see)
p +
geom_point() +
theme_modern()
p +
geom_jitter()
p +
geom_jitter(width = 0.2, height = 0.2)
p +
geom_jitter(width = 0.5, height = 0.5)
p +
geom_jitter(width = 1, height = 1)
p +
geom_jitter(width = 2, height = 0.5)
p +
geom_jitter(width = 0.5, height = 2)
p +
geom_count()
class: 車種pc <-
ggplot(mpg, aes(displ, hwy,
color = class)) # 色分けに使用する変数指定
pc + geom_point()
pc +
geom_point() +
scale_color_manual(values = c("red", "green", "blue", "yellow", "pink", "black", "brown"))
pc +
geom_point() +
scale_color_manual(values = c("red", "green", "blue", "yellow", "pink", "black", "brown"),
labels = c("二人乗り","小型","中型","ミニバン","貨物","サブコンパクト","SUV"))
?scale_color_brewerpc +
geom_point() +
scale_color_brewer(palette = "Set2")
ggplot(mpg, aes(displ, hwy,
shape = class)) +
geom_point()
pc +
geom_text(aes(label = class), show.legend = FALSE) # 凡例なし
pc +
geom_text(aes(label = class), show.legend = FALSE,
check_overlap = TRUE ) # 凡例なし
pc +
geom_text(aes(label = class), show.legend = FALSE,
angle = 45 ) # 凡例なし
mpg %>%
mutate(drv_image =
case_when(drv == "4" ~ "image/kujira.PNG",
drv == "f" ~ "image/kujira_green.PNG",
drv == "r" ~ "image/kujira_orange.PNG")) %>%
ggplot(aes(displ, hwy)) +
geom_image(aes(image = drv_image), size = .05)
p +
geom_point(aes(size = cty))
p +
geom_point(aes(alpha = cty))
### 大きさと濃さ
p +
geom_point(aes(size = cty, alpha = cty))
pc +
geom_point() +
theme(legend.position = "bottom")
pc +
geom_point() +
theme(legend.position = "none")
legend.position = で、x軸、y軸のそれぞれを0-1の範囲で表した相対的な位置の座標で表すlegend.direction = "horizontal"で、凡例を横向きに展開pc +
geom_point() +
theme(legend.position = c(.70, .80),
legend.direction = "horizontal")
pc +
geom_point() +
theme(legend.position = c(.70, .80),
legend.direction = "horizontal",
legend.background = element_blank())
pc +
geom_point() +
theme(legend.position = "bottom") +
guides(color = guide_legend(nrow = 1))
pc +
geom_point() +
guides(color = guide_legend(title = "車種"))
# こちらでもOK
# pc +
# geom_point() +
# labs(color = "車種")
pc +
geom_point() +
guides(color = guide_legend(title = ""))
# こちらでもOK
# pc +
# geom_point() +
# labs(color = "")
\nを入れるpc +
geom_point() +
guides(color = guide_legend(title = "長い\nタイトル"))
pc +
geom_point() +
guides(color = guide_legend(reverse = TRUE))
# class別のエンジンの排気量平均を確認して並び替え
mpg %>%
group_by(class) %>%
summarise(mean_hwy = mean(hwy)) %>%
arrange(mean_hwy)
## # A tibble: 7 x 2
## class mean_hwy
## <chr> <dbl>
## 1 pickup 16.9
## 2 suv 18.1
## 3 minivan 22.4
## 4 2seater 24.8
## 5 midsize 27.3
## 6 subcompact 28.1
## 7 compact 28.3
pc +
geom_point() +
scale_color_discrete(breaks = # 色分けをcolorでやっているのでscale_color
c("pickup", "suv", "minivan",
"2seater", "midsize", "subcompact", "compact"))
mpg %>%
mutate(class = fct_relevel(class,
"pickup", "suv", "minivan",
"2seater", "midsize", "subcompact", "compact")) %>%
ggplot(aes(displ, hwy,
color = class)) +
geom_point()
pc +
geom_point() +
theme(legend.key = element_blank())
geom_point()を重ね書き
filter()で選択pp <-
p +
# suvのデータのみプロット
geom_point(
data = filter(mpg, class == "suv"),
color = "blue",
size = 3) +
# 通常のプロット
geom_point()
pp
pp +
annotate(geom = "point", x = 5.5, y = 40, colour = "blue", size = 3) +
annotate(geom = "point", x = 5.5, y = 40) +
annotate(geom = "text", x = 5.6, y = 40, label = "suv", hjust = "left")
pc +
geom_point() +
facet_wrap(vars(class))
pc +
geom_point() +
facet_wrap(vars(class),
ncol = 2)
pc +
geom_point() +
facet_wrap(vars(class)) +
theme(strip.text = element_text(size = 6))
pc +
geom_point() +
facet_wrap(vars(class)) +
theme(strip.text = element_text(size = 8))
pc +
geom_point() +
facet_wrap(vars(class)) +
theme(strip.text = element_text(size = 10))
pc +
geom_point() +
facet_wrap(vars(class)) +
theme(strip.background = element_rect(colour = "black", fill = "white"))
library(cowplot)
pc_wrap <-
pc +
geom_point() +
facet_wrap(vars(class))
ggdraw(pc_wrap) +
draw_label("text", x = 0.05, y = 0.95, color = "blue") +
draw_label("text", x = 0.05, y = 0.65, color = "blue") +
draw_label("text", x = 0.05, y = 0.35, color = "blue")
gghilightパッケージを使う(ここだけで使うのでgghilight::をつける)pc +
geom_point() +
gghighlight::gghighlight() +
facet_wrap(vars(class))
pc +
geom_point() +
facet_wrap(vars(class,cyl))
pc +
geom_point() +
facet_grid(vars(cyl),vars(class))
library(knitr) # kable()を使うため
# 車種(class)と年次別の燃料効率(hwy)平均値データを作成
hwy_class_y <-
mpg %>%
group_by(class, year) %>%
summarise(mean_hwy = mean(hwy))
kable(hwy_class_y)
| class | year | mean_hwy |
|---|---|---|
| 2seater | 1999 | 24.50000 |
| 2seater | 2008 | 25.00000 |
| compact | 1999 | 27.92000 |
| compact | 2008 | 28.72727 |
| midsize | 1999 | 26.50000 |
| midsize | 2008 | 28.04762 |
| minivan | 1999 | 22.50000 |
| minivan | 2008 | 22.20000 |
| pickup | 1999 | 16.81250 |
| pickup | 2008 | 16.94118 |
| subcompact | 1999 | 29.00000 |
| subcompact | 2008 | 27.12500 |
| suv | 1999 | 17.55172 |
| suv | 2008 | 18.63636 |
hwy_class_y_w <-
hwy_class_y %>%
pivot_wider(names_from = year,
values_from = mean_hwy)
kable(hwy_class_y_w)
| class | 1999 | 2008 |
|---|---|---|
| 2seater | 24.50000 | 25.00000 |
| compact | 27.92000 | 28.72727 |
| midsize | 26.50000 | 28.04762 |
| minivan | 22.50000 | 22.20000 |
| pickup | 16.81250 | 16.94118 |
| subcompact | 29.00000 | 27.12500 |
| suv | 17.55172 | 18.63636 |
hwy_class_y_w %>%
pivot_longer(-class, # この変数以外、つまり1999と2008の列を使用
names_to = "year",
values_to = "mean_hwy")
## # A tibble: 14 x 3
## # Groups: class [7]
## class year mean_hwy
## <chr> <chr> <dbl>
## 1 2seater 1999 24.5
## 2 2seater 2008 25
## 3 compact 1999 27.9
## 4 compact 2008 28.7
## 5 midsize 1999 26.5
## 6 midsize 2008 28.0
## 7 minivan 1999 22.5
## 8 minivan 2008 22.2
## 9 pickup 1999 16.8
## 10 pickup 2008 16.9
## 11 subcompact 1999 29
## 12 subcompact 2008 27.1
## 13 suv 1999 17.6
## 14 suv 2008 18.6
ggplot(hwy_class_y,
aes(year, mean_hwy, color = class)) +
geom_line()
ggplot(hwy_class_y,
aes(year, mean_hwy, color = class)) +
geom_point() +
geom_line() +
scale_x_continuous(breaks = c(1999, 2008))
R for Data Science 24 Model building > 24.3 What affects the number of daily flights? より
library(nycflights13) #
library(lubridate) #
# 日別の便数
daily <- flights %>%
mutate(date = make_date(year, month, day)) %>%
group_by(date) %>%
summarise(n = n())
head(daily)
## # A tibble: 6 x 2
## date n
## <date> <int>
## 1 2013-01-01 842
## 2 2013-01-02 943
## 3 2013-01-03 914
## 4 2013-01-04 915
## 5 2013-01-05 720
## 6 2013-01-06 832
# 曜日変数追加
daily <- daily %>%
mutate(wday = wday(date, label = TRUE))
ds <-
daily %>%
filter(wday == "土") %>%
ggplot(aes(date, n)) +
geom_line()
ds
date_labels =の後に指定する文字の意味は?strptimeを参照ds +
scale_x_date(date_breaks = "1 month", date_labels = "%b") # 本来はJanなどの月名の省略文字
ds +
scale_x_date(date_breaks = "6 month", date_labels = "%Y/%b") # 西暦/月
# 【再掲】車種(class)と年次別の燃料効率(hwy)平均値データを作成
hwy_class_y <-
mpg %>%
group_by(class, year) %>%
summarise(mean_hwy = mean(hwy))
hwy_class_y_99 <-
hwy_class_y %>%
filter(year == 1999)
pb <-
ggplot(hwy_class_y_99,
aes(x = class, y = mean_hwy))
pb + geom_col()
fill =で指定pb + geom_col(fill = "deepskyblue3")
pb + geom_col(aes(fill = class))
color =だと枠線の色にpb + geom_col(color = "red")
library(ggpattern)
pb +
geom_col_pattern(
aes(pattern = class, pattern_angle = class, pattern_spacing = class),
fill = 'white',
colour = 'black',
pattern_spacing = 0.025,
# pattern_density = 0.4,
# pattern_fill = 'black',
# pattern_colour = 'black'
)
pb + geom_col(width = 0.5)
hwy_class_y_99 %>%
ggplot(aes(
fct_relevel(class,
"pickup", "suv", "minivan",
"2seater", "midsize", "subcompact", "compact"),
mean_hwy)) +
geom_col() +
labs(x = "class")
fct_reorder(class, mean_hwy, .desc = TRUE) ggplot(hwy_class_y_99,
aes(x = fct_reorder(class, mean_hwy),
y = mean_hwy)) +
geom_col()
pb +
geom_col() +
theme(axis.text.x = element_text(angle = 45,
hjust = 1))
pb +
geom_col() +
geom_text(aes(label = mean_hwy))
pb +
geom_col() +
geom_text(aes(label = round(mean_hwy, 1)), # 値ラベルを丸める
vjust = -1, # 値をグラフの上に表示
size = 3.5) + # 値の文字のサイズ
scale_y_continuous(limits = c(0, 30)) # y軸の範囲を指定しないとラベルが隠れるため
vjust =の値を大きくするほど下の位置に移動するpb +
geom_col() +
geom_text(aes(label = round(mean_hwy, 1)), # 値ラベルを丸める
vjust = 1.5, # 値をグラフの中に表示
size = 4, # 文字のサイズ
color = "white") # 文字の色
pb +
geom_col() +
geom_vline(xintercept = 5,
linetype = "dotted",
color = "red",
size = 1)
pb +
geom_col() +
annotate(geom = "text",
x = 4.5, y = 28, # テキストの中心座標位置
label = "ここにテキスト",
color = "blue",
size = 7)
pb +
geom_col() +
coord_flip()
pb +
geom_col() +
annotate(geom = "text",
x = 7.5, y = -5, # テキストの中心座標位置,表示されるグラフの見かけはXYが入れ替わる
color = "blue",
size = 6,
label = "テキスト") +
coord_flip(xlim = c(1, 7.2),
ylim = c(0, 30),
clip = "off")
pby <-
ggplot(hwy_class_y,
aes(x = class, y = mean_hwy, fill = factor(year)))
pby +
geom_col(position = "dodge") +
labs(fill = "year") # 凡例のタイトル名をyearに戻す
pby +
geom_col(position = "dodge") +
scale_fill_manual(values = c("violetred3", "deepskyblue3"))
pby +
geom_col(position = "dodge") +
geom_text(aes(label = round(mean_hwy, 1)),
vjust = -1,
position = position_dodge(width = 0.9), # ラベルを棒ごとに表示させる
size = 3.5) +
scale_y_continuous(limits = c(0, 30))
pbar <-
ggplot(mpg, aes(x = class))
pbar + geom_bar()
pbar +
geom_bar() +
geom_text(aes(label = ..count..),
stat = "count",
vjust = 1.5,
color = "white") # 棒の上に黒い文字で出したい場合はこれを削除し、vjustを0.5に
x = factor(1)と固定するggplot(mpg,
aes(x = factor(1), fill = class)) +
geom_bar() +
labs(x = "class") +
geom_text(aes(label = ..count..),
stat = "count",
position = position_stack(vjust = 0.5)) +
theme(axis.text.x = element_blank()) # 軸の1を消す
# まずパーセントのデータを別途作成
percent <-
mpg %>%
count(class) %>%
mutate(pct = scales::percent_format(accuracy = 0.1)(n/sum(n)))
# グラフ
ggplot(mpg, aes(x = factor(1), fill = factor(class))) +
geom_bar(position = "fill") +
geom_text(data = percent, # 上で作ったパーセントのデータ指定
aes(y = n, label = pct),
position = position_fill(vjust = 0.5)) +
scale_y_continuous(labels = scales::label_percent(accuracy = 0.1)) + # y軸を%表記に
theme(axis.text.x = element_blank()) + # x軸の1を消す
labs(x = "class") # x軸の名前をclassに
drv:ドライブトレインのタイプ
pbar +
geom_bar(aes(fill = drv))
pbar +
geom_bar(aes(fill = factor(drv))) +
geom_text(aes(fill = factor(drv),
label = ..count..),
stat = "count",
position = position_stack(vjust = 0.5))
pbar +
geom_bar(aes(fill = drv),
position = "fill") +
scale_y_continuous(labels = scales::label_percent(accuracy = 0.1)) # 縦軸を%表記に
accuracy = 0.1をつける
# まずグラフ上に表示するテキストラベルのためにパーセントのデータを別途作成
df_pct <-
mpg %>%
group_by(class) %>%
count(class, drv) %>%
mutate(pct_num = n/sum(n),
pct = scales::percent_format(accuracy = 0.1)(pct_num))
# old
# scales::percent(pct_num)
# グラフ
ggplot(mpg, aes(x = factor(class), fill = factor(drv))) +
geom_bar(position = "fill") +
geom_text(data = df_pct, # 上で作ったパーセントのデータ指定
aes(y = n, label = pct),
position = position_fill(vjust = 0.5)) +
scale_y_continuous(labels = scales::label_percent(accuracy = 0.1)) # 縦軸を%表記に
pbar +
geom_bar(aes(fill = drv), position = "dodge")
pbar +
geom_bar(aes(fill = drv), position = "dodge2")
position_dodge()の中で指定していくpbar +
geom_bar(aes(fill = drv),
position = position_dodge(preserve = "single"))
position_dodge2()pbar +
geom_bar(aes(fill = drv),
position = position_dodge2(preserve = "single",
pad = 0.3))
ggplot(mpg,
aes(x = factor(1), fill = class)) +
geom_bar() +
coord_polar(theta = "y")
ggplot(mpg,
aes(x = factor(1), fill = class)) +
geom_bar() +
scale_fill_brewer(palette = "Set2") +
coord_polar(theta = "y")
ggplot(mpg,
aes(x = factor(1), fill = class)) +
geom_bar() +
scale_fill_brewer(palette = "Set2") +
coord_polar(theta = "y") +
theme_classic()
ggplot(mpg, aes(x = factor(1), fill = factor(class))) +
geom_bar(position = "fill") +
coord_polar(theta = "y")
library(ggpubr)
# パーセント(%の前の部分の数字)のデータを別途作成
percent <-
mpg %>%
count(class) %>%
mutate(pct = n/sum(n)*100,
pctp = scales::percent_format(accuracy = 0.1)(n/sum(n))) # %付きの値も作成
ggdonutchart(percent, "pct", label = "class",
fill = "class",
color = "white", # 区分線の色
lab.pos = "in",
lab.font = c(3, "white")) + # ラベルの文字サイズ、色
scale_fill_brewer(palette = "Set2")
ggbeeswarmパッケージを使う(ここだけで使うのでggbeeswarm::をつける)pch <-
ggplot(mpg, aes(class, hwy))
pch +
ggbeeswarm::geom_beeswarm(aes(color = class)) # 色分けに指定する変数
pch +
geom_violin(aes(fill = class)) # 色分けに指定する変数
pch + geom_violin(aes(fill = class),
alpha = 0.5)
seeパッケージのgeom_violindot()を使うpch +
geom_violindot(aes(fill = class),
size_dots = 5) # ドットサイズ変更
library(ggmosaic)
mpg %>%
mutate(cyl = factor(cyl)) %>% # 変数を因子型に
ggplot() +
geom_mosaic(aes(x = product(cyl),
fill = cyl))
mpg %>%
mutate(drv = factor(drv),
class = factor(class)) %>% # 変数を因子型に
ggplot() +
geom_mosaic(aes(x = product(drv,class),
fill = drv))
library(gridExtra)
p1 <- p + geom_point()
p2 <-
pc +
geom_point() +
theme(legend.text = element_text(size = 6),# 凡例のテキストを小さく
legend.position = "bottom", # 凡例を下に
legend.background = element_blank()) # 凡例のボックス背景を透明に
grid.arrange(p1, p2, nrow = 1) # 1行に
grid.arrange(p1, p2, ncol = 1) # 1列に
p3 <-
pc +
geom_point() +
facet_wrap(vars(class)) +
theme(legend.position = "none") # 凡例を消す
grid.arrange(p1, p2, p3, ncol = 2) # 2列に